﻿/*-----------------------------------------------
// File: BCBeUltimatedState.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;

namespace Battle.View
{

    public class BCBeUltimatedState : Fsm.FsmStateMonoBase<ActorState>
    {
        public static int BeUltimateFriendIndex = 0;
        public static int BeUltimateEnemyIndex = 0;

        public float behitTimer = 0;
        public float behitLength = 1;

        public float beflyHitTime = 0;

        BattleCharacter bc {
            get { return this.controller as BattleCharacter; }
        }
        public BCBeUltimatedState(ActorState state, BattleCharacter ctrl) : base(state, ctrl) {
        }

        public override void Enter()
        {
            var currentPackage = BattleViewCtrl.GetInstance().GetUltimatePackage();
            var sourceBC = BattleViewCtrl.GetInstance().GetBattleCharacter(currentPackage.actorUid);
            var ultimateLogic = BattleViewCtrl.GetInstance().GetUltimateLogic(sourceBC.GetSkill(SkillType.Ultimate).HSId);
            if(ultimateLogic == null)
            {
                isInStraight = false;
                curState = BeHitState.None;
                bc.OnEnterUltimate();
                return;
            }
            Transform parentTransform = null;
            var ultiData = ultimateLogic.GetComponent<UltimateData>();
            if (bc.camp == sourceBC.camp)
            {
                if (BeUltimateFriendIndex < ultiData.friendOptionPoints.Count)
                {
                    parentTransform = ultimateLogic.GetComponent<UltimateData>().friendOptionPoints[BeUltimateFriendIndex];
                    BeUltimateFriendIndex++;
                }
            }
            else {
                if (BeUltimateEnemyIndex < ultiData.enemyOptionPoints.Count) {
                    parentTransform = ultiData.enemyOptionPoints[BeUltimateEnemyIndex];
                    BeUltimateEnemyIndex++;
                }
            }
            if (parentTransform != null)
            {
                bc.transform.SetParent(parentTransform);
            }
            else {
                bc.transform.SetParent(ultiData.transform);
                bc.gameObject.SetActive(false);
            }
            var package = BattleViewCtrl.GetInstance().GetUltimatePackage();
            var sourcebc = BattleViewCtrl.GetInstance().GetBattleCharacter(package.actorUid);
            var skill = sourcebc.GetSkill(SkillType.Ultimate) as UltimateSkill;
            if (skill.speDealInfo.ContainsKey(bc.heroId))
            {
                bc.transform.localPosition = Vector3.zero + skill.speDealInfo[bc.heroId].offset;
                bc.transform.localScale = Vector3.one * skill.speDealInfo[bc.heroId].scale;
            }
            else {
                bc.transform.localPosition = Vector3.zero;
                bc.transform.localScale = Vector3.one;
            }

            bc.transform.localEulerAngles = Vector3.zero;
            bc.PlayStand();
            behitLength = bc.anim.anim[AniName.BEHIT].length;
            behitTimer = 0;
            beflyHitTime = 5 / 30f;

            isInStraight = false;
            curState = BeHitState.None;
            bc.OnEnterUltimate();
        }

        public override void Leave()
        {
            bc.PlayStand(true);
            bc.OnLeaveUltimate();
        }



        private BeHitState curState = BeHitState.None;
        public enum BeHitState {
            None,
            Normal,
            Floor,
            Ground,
        }

        bool isInStraight = false;

        public void OnSkillRelease(BattleSkillRelease release) {
            if (release.causeSpecialState == ActorSpecialState.FLOOR)
            {
                BeFly();
            }
            else if (release.causeSpecialState == ActorSpecialState.GROUND) {
                BeGround();
            }
            else {
                Behit(release.beHitTime);
            }
        }

        public void BeFly() {
            bc.anim.anim[AniName.FLOAT_FLY].speed = 1;
            bc.anim.Play(AniName.FLOAT_FLY, WrapMode.Clamp, true);
            curState = BeHitState.Floor;
        }

        public void BeGround() {
            bc.anim.anim[AniName.FALL_DOWN].speed = 1;
            bc.anim.Play(AniName.FALL_DOWN, WrapMode.Clamp, true);
            curState = BeHitState.Ground;
        }


        public void Behit(float bTime)
        {
            if (curState == BeHitState.Normal)
            {
                // hit过程，僵直时间已经过了，进入新的一轮hit
                if (!isInStraight)
                {
                    behitTimer = bTime - behitLength;
                    bc.anim.anim[AniName.BEHIT].speed = 1;
                    bc.anim.Play(AniName.BEHIT, WrapMode.Clamp, true);
                    isInStraight = true;
                }
            }
            else if (curState == BeHitState.Floor)
            {
                bc.anim.Play(AniName.FLOAT_FLY, WrapMode.Clamp, true);
                bc.anim.anim[AniName.FLOAT_FLY].time = beflyHitTime;
            }
            else if (curState == BeHitState.Ground) {
                bc.anim.Play(AniName.FALL_HIT, WrapMode.Clamp, true);
            }
            else {
                // none,进入hit过程
                behitTimer = bTime - behitLength;
                bc.anim.anim[AniName.BEHIT].speed = 1;
                bc.anim.Play(AniName.BEHIT, WrapMode.Clamp, true);
                isInStraight = true;
                curState = BeHitState.Normal;
            }
        }

        public override void Update()
        {
            if (curState == BeHitState.Normal)
            {
                if (behitTimer > 0)
                {
                    if (bc.anim.anim[AniName.BEHIT].normalizedTime >= 0.5f)
                    {
                        behitTimer -= Time.deltaTime;
                        if (behitTimer <= 0)
                        {
                            bc.anim.anim[AniName.BEHIT].speed = 1;
                            isInStraight = false;
                        }
                        else {
                            bc.anim.anim[AniName.BEHIT].speed = 0;
                            isInStraight = true;
                        }
                    }
                    else {
                        isInStraight = true;
                    }
                }
                else {
                    isInStraight = false;
                    if (bc.anim.anim[AniName.BEHIT].normalizedTime >= 1f)
                    {
                        curState = BeHitState.None;
                        return;
                    }
                }
            }
            else if (curState == BeHitState.Floor)
            {
                if (bc.anim.anim[AniName.FLOAT_FLY].normalizedTime >= 1f)
                {
                    curState = BeHitState.None;
                }
            }
            else if (curState == BeHitState.Ground) {
            }
        }

    }
}